<template>
  <div class="top-nav-bar">
    <div class="nav-bar" @mouseleave="hoverIndex = null">
      <template v-for="(item, idx) in dockItems">
        <div
          v-if="item.type === 'icon'"
          :key="item.label"
          class="nav-item"
          :class="{
            'is-hovered': hoverIndex === idx,
            'is-adjacent': hoverIndex === idx - 1 || hoverIndex === idx + 1
          }"
          @mouseenter="hoverIndex = idx"
          @focus="hoverIndex = idx"
          @mouseleave="hoverIndex = null"
          @click="item.action ? item.action() : null"
          tabindex="0"
        >
          <div class="nav-tooltip" v-if="hoverIndex === idx">{{ item.label }}</div>
          <a
            v-if="item.href && !item.action"
            :href="item.href"
            target="_blank"
            rel="noopener noreferrer"
            class="nav-link"
            :aria-label="item.label"
          >
            <component :is="item.icon" class="nav-icon" />
          </a>
          <component v-else :is="item.icon" class="nav-icon" />
        </div>
        <div v-else-if="item.type === 'divider'" :key="'divider-' + idx" class="nav-divider"></div>
      </template>
      <div
        class="nav-item"
        :class="{ 'is-hovered': hoverIndex === dockItems.length }"
        @mouseenter="hoverIndex = dockItems.length"
        @focus="hoverIndex = dockItems.length"
        @mouseleave="hoverIndex = null"
        @click="toggleTheme"
        tabindex="0"
      >
        <div class="nav-tooltip" v-if="hoverIndex === dockItems.length">
          {{ isDark ? '明亮' : '暗黑' }}
        </div>
        <component :is="isDark ? Moon : Sunny" class="nav-icon" />
      </div>
    </div>
  </div>
  <!-- 写文章弹窗 -->
  <el-dialog v-model="addDialogVisible" title="写文章" width="400px">
    <el-form :model="addForm" label-width="60px">
      <el-form-item label="标题">
        <el-input v-model="addForm.title" />
      </el-form-item>
      <el-form-item label="内容">
        <el-input
          v-model="addForm.content"
          type="textarea"
          :rows="4"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="addDialogVisible = false">取消</el-button>
      <el-button type="primary" @click="submitAdd">发布</el-button>
    </template>
  </el-dialog>
  
  <!-- 音乐播放器 -->
  <MusicPlayer :visible="musicPlayerVisible" @close="musicPlayerVisible = false" />
</template>

<script setup>
import { ref, defineComponent } from 'vue'
import { House, Edit, Promotion, Sunny, Headset, User, Moon } from '@element-plus/icons-vue'
import { useRouter } from 'vue-router'
import { SwitchButton } from '@element-plus/icons-vue'
import { ElMessageBox, ElMessage } from 'element-plus'
import { useUserStore } from '@/store/store'
import request from '@/request/request'
import MusicPlayer from './MusicPlayer.vue'

// 写文章弹窗相关
const addDialogVisible = ref(false)
const addForm = ref({ title: '', content: '' })

// 自定义SVG图标
const LinkedInIcon = defineComponent({
  name: 'LinkedInIcon',
  template: `<svg viewBox="0 0 24 24"><title>LinkedIn</title><path fill="currentColor" d="M20.447 20.452h-3.554v-5.569c0-1.328-.027-3.037-1.852-3.037-1.853 0-2.136 1.445-2.136 2.939v5.667H9.351V9h3.414v1.561h.046c.477-.9 1.637-1.85 3.37-1.85 3.601 0 4.267 2.37 4.267 5.455v6.286zM5.337 7.433c-1.144 0-2.063-.926-2.063-2.065 0-1.138.92-2.063 2.063-2.063 1.14 0 2.064.925 2.064 2.063 0 1.139-.925 2.065-2.064 2.065zm1.782 13.019H3.555V9h3.564v11.452zM22.225 0H1.771C.792 0 0 .774 0 1.729v20.542C0 23.227.792 24 1.771 24h20.451C23.2 24 24 23.227 24 22.271V1.729C24 .774 23.2 0 22.222 0h.003z"/></svg>`
})
const XIcon = defineComponent({
  name: 'XIcon',
  template: `<svg viewBox="0 0 24 24"><title>X</title><path fill="currentColor" d="M18.901 1.153h3.68l-8.04 9.19L24 22.846h-7.406l-5.8-7.584-6.638 7.584H.474l8.6-9.83L0 1.154h7.594l5.243 6.932ZM17.61 20.644h2.039L6.486 3.24H4.298Z"/></svg>`
})

const router = useRouter()
const hoverIndex = ref(null)

// 点击家按钮，回到首页
function goHome() {
  router.push("/")
  window.scrollTo(0, 0);
}

// 点击写文章按钮，出现写文章表单
function addArticle() {
  addDialogVisible.value = true
}

// 点击去看大家的文章，跳转至文章列表页
function goArticleList() {
  router.push('/AllWenZhang')
}

// 点击退出按钮，退出登录
function logout() {
  ElMessageBox.confirm('确定要退出登录吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  }).then(() => {
    useUserStore().removeToken();
    router.push('/login');
    window.location.reload();
  });
}

// 发布文章
async function submitAdd() {
  try {
    // 这里对接后端接口
    await request.post('/WanZhang', addForm.value)
    ElMessage.success('发布成功')
    addDialogVisible.value = false
    addForm.value = { title: '', content: '' }
    // 刷新页面
    window.location.reload();
  } catch (e) {
    ElMessage.error('发布失败')
  }
}

// 点击自我按钮，跳转至自我页
function goMy() {
  router.push('/My')
}

// 1. 初始化时读取 localStorage
const isDark = ref(localStorage.getItem('themeMode') === 'dark')

// 2. 页面加载时自动设置主题
if (isDark.value) {
  document.body.classList.add('dark-theme')
}

// 3. 切换主题时同步 localStorage
function toggleTheme() {
  isDark.value = !isDark.value
  if (isDark.value) {
    document.body.classList.add('dark-theme')
    localStorage.setItem('themeMode', 'dark')
  } else {
    document.body.classList.remove('dark-theme')
    localStorage.setItem('themeMode', 'light')
  }
}

// 音乐播放器相关
const musicPlayerVisible = ref(false)

const dockItems = [
  { type: 'icon', label: '家', icon: House, action: goHome },
  { type: 'icon', label: '写文章', icon: Edit, action: addArticle },
  { type: 'divider' },
  { type: 'icon', label: '去看大家的文章', icon: Promotion, action: goArticleList },
  { type: 'icon', label: '自我', icon: User, action: goMy },
  { type: 'icon', label: '听歌', icon: Headset, action: () => musicPlayerVisible.value = true },
  { type: 'icon', label: '退出', icon: SwitchButton, action: logout },
  { type: 'divider' }
]
</script>

<style scoped>
.top-nav-bar {
  position: fixed;
  left: 0;
  right: 0;
  top: 40px;
  display: flex;
  justify-content: center;
  z-index: 99;
  pointer-events: none;
}
.nav-bar {
  display: flex;
  align-items: center;
  background: #fff;
  border-radius: 24px;
  box-shadow: none;
  padding: 8px 24px;
  gap: 0;
  pointer-events: auto;
  border: 1.5px solid #ededed;
  transition: border-color 0.25s, transform 0.25s;
  width: auto;
}
.nav-item {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 48px;
  height: 48px;
  border-radius: 50%;
  font-size: 24px;
  color: #222;
  cursor: pointer;
  background: transparent;
  transition: background 0.2s, box-shadow 0.2s, transform 0.2s;
  z-index: 1;
}
.nav-link {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
  color: inherit;
}
.nav-icon {
  width: 24px;
  height: 24px;
  transition: transform 0.;
}
.nav-item.is-hovered {
  background: #f5f5f5;
  box-shadow: 0 4px 16px 0 rgba(0,0,0,0.10);
  transform: scale(1.12);
  z-index: 2;
}
.nav-item.is-adjacent {
  transform: scale(1.06);
  z-index: 1;
}
.nav-tooltip {
  position: absolute;
  top: -28px;
  left: 50%;
  transform: translateX(-50%);
  background: #222;
  color: #fff;
  border-radius: 10px;
  padding: 3px 12px;
  font-size: 14px;
  font-weight: 500;
  opacity: 1;
  pointer-events: none;
  transition: opacity 0.2s, top 0.2s;
  white-space: nowrap;
  z-index: 10;
}
.nav-divider {
  width: 1px;
  height: 32px;
  background: #ededed;
  margin: 0 10px;
  border-radius: 1px;
}
</style>
<style>
body.dark-theme {
  background: #23272e !important;
}
body.dark-theme .common-layout {
  background: linear-gradient(120deg, #23272e 0%, #444950 100%) !important;
}
body.dark-theme .el-card,
body.dark-theme .el-main,
body.dark-theme .el-header {
  background: #23272e !important;
  color: #eee !important;
}
body.dark-theme .vertical-article {
  background: #23272e !important;
  color: #eee !important;
}
</style> 